<template>
	<!-- 订单添加 -->
	<div>
		<div class="edit-top">
			添加信息
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<div class="edit-title">订单信息</div>
			<div class="edit-item">
				<label>车牌号码</label><el-select v-model="value" filterable remote reserve-keyword placeholder="请输入车牌号码" :remote-method="remoteMethod">
					<el-option v-for="(item,index) in options" :key="item.id" :label="item.licensePlateNumber" :value="item">
					</el-option>
				</el-select>
			</div>
			<!-- <div class="edit-item" v-if="is_show">
				<label>车架号</label><input v-model="info.vehicle.vin" type="text" disabled="disabled" />
			</div>
			<div class="edit-item" v-if="is_show">
				<label>用户账号</label><input v-model="info.client.sysUser.userName" type="text" disabled="disabled" />
			</div> -->
			<div class="edit-title">换电前</div>
			<div class="edit-item" >
				<label>总公里数</label><input v-model="param.preSwitchTotalMileage" type="text" />
			</div>
			<div class="edit-item" >
				<label>剩余公里数</label><input v-model="param.preSwitchRemainingMileage" type="text" />
			</div>
			<!-- <div class="edit-title">换电后</div>
			<div class="edit-item" >
				<label>换电公里数</label><input v-model="param.switchMileage" type="text" />
			</div>
			<div class="edit-item" >
				<label>剩余公里数</label><input v-model="param.afterSwitchRemainingMileage" type="text" />
			</div> -->
		</div>
		<div class="edit-bottom">
			<span @click='cancel'>取消</span>
			<el-button @click='save' type="primary">添加</el-button>
		</div>
	</div>
</template>

<script>
	import Global from '@/components/Global.vue'
	export default {
		data() {
			return {
				options: [],
				is_show:false,
				value: '',
				dict: '',
				station_list: [],
				info: {},
				param:{
					preSwitchTotalMileage:'',
					preSwitchRemainingMileage:'',
					// afterSwitchRemainingMileage:'',
					// switchMileage:'',
					memberId:'',
					memeberUsername:'',
					orderType:0,
					sourceType:0,
					stationId:'',
					switchTime:'',
					vehicleId:'',
					vehicleLicenseNumber:'',
					vehicleVin:'',
				}
			}
		},
		mounted() {
			this.get_dict();
			this.get_station_list();
		},
		watch:{
			value(){
				if(this.value != ''){
					var self = this;
					this.$axios.get(this.$global.serverIp + 'vehicle/vehicle/plateOrVinGetInfo/'+this.value.id)
						.then(function(res) {
							if (res != '')
								self.info = res.data;
								self.param.vehicleId = res.data.vehicle.id;
								self.param.vehicleLicenseNumber = res.data.vehicle.licensePlateNumber;
								self.param.vehicleVin = res.data.vehicle.vin;
								self.param.memeberUsername = res.data.client.sysUser.userName;
								self.param.memberId = res.data.client.userId;
								self.param.stationId = res.data.station.id;
								console.log(self.param);
								// self.is_show = true;
						})
				}
			}
		},
		methods: {
			remoteMethod(query) {
				if(query == '')return;
				var self = this;
				this.$axios.get(this.$global.serverIp + 'vehicle/vehicle/plateOrVinGet/'+query)
					.then(function(res) {
						if (res != '')
						self.options = res.data;
							console.log(res);
					})
			},
			get_station_list() {
				var self = this;
				this.$axios.post(this.$global.serverIp + 'station/station/findPage', {
						"limit": "10000",
						"page": "1"
					})
					.then(function(res) {
						if (res != '') {
							self.station_list = res.data.list;
							self.info.stationId = self.station_list[0].id;
						}

					})
			},
			get_dict() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=battery')
					.then(function(res) {
						if (res != '')
							self.dict = res.data;
					})
			},
			save() {
				if (this.param.vehicleLicenseNumber == "") {
					this.$popup.open('车牌号码不能为空');
					return;
				}
				if (this.param.preSwitchTotalMileage == "") {
					this.$popup.open('总公里数不能为空');
					return;
				}
				if (this.param.preSwitchRemainingMileage == "") {
					this.$popup.open('换电前剩余公里数不能为空');
					return;
				}
				// if (this.param.switchMileage == "") {
				// 	this.$popup.open('换电公里数不能为空');
				// 	return;
				// }
				// if (this.param.afterSwitchRemainingMileage == "") {
				// 	this.$popup.open('换电后剩余公里数不能为空');
				// 	return;
				// }
				var self = this;
				this.$axios.post(this.$global.serverIp + 'order/order/startOrder', self.param)
					.then(function(res) {
						if (res != '') {
							self.success('添加成功');
						}
					})
			},
			success(content) {
				var self = this;
				this.$alert(content, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						self.$emit("saveAdd", 2);
					}
				});
			},
			cancel() {
				this.$emit("saveAdd", 1);
			}
		}
	}
	import '@/assets/css/edit.css'
</script>
